# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import datetime
import time
from ciso8601 import parse_datetime
import datetime
import pytz


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press ⌘F8 to toggle the breakpoint.

def local2utc(local_dtm):
    # 本地时间转 UTC 时间（ -8:00 ）
    return datetime.datetime.utcfromtimestamp(local_dtm.timestamp())


def utc_2_pk(utctime_str: str) -> datetime:
    '''UTC时间字符串转化为北京时间的datetime对象
    :参数 utctime_str:UTC时间字符串，格式为yyyy-m-d H
    '''
    # 构造出没有时区的datetime对象
    naive_time = datetime.datetime.strptime(utctime_str, '%Y-%m-%d %H:%M:%S')
    # 取出上述对象的年月日小时构造一个时区为utc的datetime对象
    utctime = datetime.datetime(naive_time.year, naive_time.month, naive_time.day, naive_time.hour,
                                tzinfo=datetime.timezone.utc)
    # 把时区为utc的对象转化为时区为Asia/Shanghai的datetime对象
    beijing = datetime.timezone(datetime.timedelta(hours=8))
    pktime = utctime.astimezone(beijing)
    return pktime


def pk_2_utc(pktime_str: str) -> datetime:
    '''北京时间字符串转化为UTC时间的datetime对象
    :参数 pktime_str:北京时间字符串，格式为yyyy-m-d H
    '''
    # 构造出没有时区的datetime对象
    naive_time = datetime.datetime.strptime(pktime_str, '%Y-%m-%d %H:%M:%S')
    # 将上述对象转化为时区为Asia/Shanghai的datetime对象
    beijing = datetime.timezone(datetime.timedelta(hours=8))
    pktime = naive_time.astimezone(beijing)
    # 将时区为上海的datetime对象转化为时区为utc的时间对象
    utctime = pktime.astimezone(datetime.timezone.utc)
    return utctime

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

    utc_tz = pytz.timezone('UTC')
    # print(datetime.now(tz=utc_tz).isoformat())
    utc_tm = datetime.datetime.fromisoformat('2021-04-10')
    print(type(utc_tm))
    print(local2utc(utc_tm))
    utc_tm = datetime.datetime.fromisoformat('2021-04-11')
    print(local2utc(utc_tm))

    print(utc_2_pk('2021-04-09 16:00:00'))
    print(pk_2_utc('2021-04-10 00:00:00'))



    #
    # datetime.datetime
    #
    # print(parse_datetime(datetime.datetime.utcnow().isoformat()))

    # st = datetime.datetime.sprptime(utc_time, '%Y-%m-%dT%H:%M:%S%z')
    # timestamp = int(time.mktime(st.timetuple()))
    # print(timestamp)
    # print(datetime.datetime.utcfromtimestamp(utc_time))

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
